﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using BlobTowerDefense.Tools;

namespace BlobTowerDefense
{
    public partial class HighScoresPage : PhoneApplicationPage
    {
        public HighScoresPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            BitmapImage imageBackground;
            if ((e.Orientation & PageOrientation.Landscape) == (PageOrientation.Landscape))
            {
                imageBackground = new BitmapImage(new Uri("../Images/Backgrounds/Background_HighScores_Landscape.png", UriKind.Relative));
            }
            else
            {
                imageBackground = new BitmapImage(new Uri("../Images/Backgrounds/Background_HighScores_Portrait.png", UriKind.Relative));
            }

            if (imageBackground != null)
            {
                ((ImageBrush)LayoutRoot.Background).ImageSource = imageBackground;
            }
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            animation_Menu_Hide_FromUp.Completed += new EventHandler(animation_Menu_Hide_FromUp_Completed);
            animation_Menu_Hide_FromUp.Begin();
            e.Cancel = true;
        }

        void animation_Menu_Hide_FromUp_Completed(object sender, EventArgs e)
        {
            animation_Menu_Hide_FromUp.Completed -= new EventHandler(animation_Menu_Hide_FromUp_Completed);
            NavigationService.Navigate(new Uri("/Pages/MainPage.xaml", UriKind.Relative));
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            animation_Menu_Show_FromDown.Begin();

            string[] names = Loader.Load_HighScore_Names();
            int[] scores = Loader.Load_HighScore_Scores();
            if (names != null && scores != null)
            {
                for (int i = 0; i < names.Length; i++)
                {
                    TextBlock textName = new TextBlock();
                    textName.FontSize = 20;
                    textName.Text = names[i];

                    TextBlock textScore = new TextBlock();
                    textScore.Text = scores[i].ToString();

                    ContentPanel.Children.Add(textName);
                    ContentPanel.Children.Add(textScore);

                    Grid.SetRow(textName, i + 1);
                    Grid.SetRow(textScore, i + 1);
                    Grid.SetColumn(textName, 0);
                    Grid.SetColumn(textScore, 1);
                }
            }

            base.OnNavigatedTo(e);
        }
    }
}